<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}部门`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.rowData"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="上级部门" prop="pid">
        <el-tree-select
          v-model="drawerProps.rowData.pid"
          :data="drawerProps.deptTree"
          :props="{ label: 'name', value: 'id' }"
          :default-expanded-keys="[drawerProps.rowData.pid]"
          :check-strictly="true"
          value-key="id"
          node-key="id"
          filterable
          clearable
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="drawerProps.rowData.name" placeholder="部门名称" clearable />
      </el-form-item>
      <el-form-item label="负责人" prop="manage_user_id">
        <el-select v-model="drawerProps.rowData.manage_user_id" placeholder="部门负责人" clearable>
          <el-option v-for="item in drawerProps.userList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="drawerProps.rowData.sort" :min="0" placeholder="部门排序" style="width: 180px;" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="drawerProps.rowData.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance, ElTreeSelect } from "element-plus";

const rules = reactive<Record<string, any[]>>({
  name: [{ required: true, message: "请输入部门名称" }],
  manage_user_id: [{ required: true, message: "请选择部门负责人" }],
  status: [{ required: true, message: "请选择菜单状态" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  deptTree: any[];
  userList: any[];
  rowData?: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => Promise<any>;
}

// drawer框状态
const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  deptTree: [],
  userList: []
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps): void => {
  if (params.rowData?.pid == 0) {
    params.rowData.pid = '';
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      delete drawerProps.value.rowData.children;
      if (!drawerProps.value.rowData.pid) {
        drawerProps.value.rowData.pid = 0;
      }
      await drawerProps.value.api!(drawerProps.value.rowData);
      ElMessage.success({ message: `${drawerProps.value.title}部门成功！` });
      if (drawerProps.value.getTableList) {
        await drawerProps.value.getTableList();
      }
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
